﻿using GalaSoft.MvvmLight.Messaging;
using messageData;
using chatGUI.DataBase.Dao;
using System;
using System.Collections.Generic;
using chatGUI.DataBase.Entity;

namespace chatGUI.SingeltonData
{
    public sealed class dataBaseOperate
    {
        private static readonly Lazy<dataBaseOperate> _instance = new Lazy<dataBaseOperate>(() => new dataBaseOperate());
        public static dataBaseOperate Instance => _instance.Value;
        private dataBaseOperate()
        {
            Messenger.Default.Register<List<messageData.User>>(this, "friendData", initDataBaseFriends);
            Messenger.Default.Register<List<messageData.Group>>(this, "groupData", initDataBaseGroups);
            Messenger.Default.Register<List<CommunicateMessage>>(this, "allMessagesBetweenTwoUser", initDataBaseUsersMessage);
            Messenger.Default.Register<List<CommunicateMessage>>(this, "allMessagesInGourp", initDataBaseGroupsMessage);
            Messenger.Default.Register<CommunicateMessage>(this, "textMessage", recieveTextMessage);
            Messenger.Default.Register<CommunicateMessage>(this, "gifMessage", recieveGifMessage);
            Messenger.Default.Register<CommunicateMessage>(this, "imageMessage", recieveImageMessage); 
            Messenger.Default.Register<CommunicateMessage>(this, "fileMessage", recieveFileMessage);
            Messenger.Default.Register<string>(this, "deleteFriend", deleteFriend);
            Messenger.Default.Register<string>(this, "withdrawGroup", withdrawGroup);
        }
        
        public void requestFriendListDataBase(string uid)
        {
            
        }
        public void requestDataBaseUsersMessage(messageData.User user)
        {
            
        }
        public void requestDataBaseGroupMessage(messageData.Group group)
        {
           
        }
        public void requestDataBaseAddFriend(string uid)
        {
            
        }
        public void requestDataBaseAddGroup(string gid)
        {
            
        }
        public void requestDataBaseDeleteFriend(string uid)
        {
            
        }
        public void requestDataBaseWithdrawGroup(string gid)
        {
            
        }



        private void initDataBaseFriends(List<messageData.User> users)
        {
            foreach(messageData.User user in users)
            {
                if (FriendDao.Update(new FriendList
                {
                    Uid = userData.Instance.user.uid,
                    Fid = user.uid,
                    AvatarUrl = miniClientData.Instance.client.file_helper.path + @"\image\groups_icon.png",
                    NickName = user.uname
                }) == 0)
                {
                    FriendDao.Insert(new FriendList
                    {
                        Uid = userData.Instance.user.uid,
                        Fid = user.uid,
                        AvatarUrl = miniClientData.Instance.client.file_helper.path + @"\image\groups_icon.png",
                        NickName = user.uname
                    });
                }
            }
        }
        private void initDataBaseGroups(List<messageData.Group> groups)
        {
            foreach (messageData.Group group in groups)
            {
                if(FriendDao.Update(new FriendList
                {
                    Uid = userData.Instance.user.uid,
                    Fid = group.gid,
                    AvatarUrl = miniClientData.Instance.client.file_helper.path + @"\image\groups_icon.png",
                    NickName = group.gname
                }) == 0)
                {
                    FriendDao.Insert(new FriendList
                    {
                        Uid = userData.Instance.user.uid,
                        Fid = group.gid,
                        AvatarUrl = miniClientData.Instance.client.file_helper.path + @"\image\groups_icon.png",
                        NickName = group.gname
                    });
                }
            }
        }
        private void initDataBaseUsersMessage(List<CommunicateMessage> messages)
        {

            foreach (var message in messages)
            {
                if (message.dataType == messageType.sentence)
                    Console.WriteLine((string)message.Data[0] + " " + message.receiver + " " + message.sender);
            }
        }
        private void initDataBaseGroupsMessage(List<CommunicateMessage> messages)
        {
            foreach (var message in messages)
            {
                if (message.dataType == messageType.sentence)
                    Console.WriteLine(message.Data[0]);
            }
        }
        private void recieveTextMessage(CommunicateMessage communication_message)
        {
            InfoDao.Save(new DataBase.Entity.Information
            {
                Owner = userData.Instance.user.uid,
                Sender = communication_message.sender,
                Receiver = communication_message.receiver,
                MDate = communication_message.date.ToString(),
                Message = communication_message.Data[0].ToString(),
                DataType = "text"
            });
        }
        private void recieveGifMessage(CommunicateMessage communication_message)
        {
            InfoDao.Save(new DataBase.Entity.Information
            {
                Owner = userData.Instance.user.uid,
                Sender = communication_message.sender,
                Receiver = communication_message.receiver,
                MDate = communication_message.date.ToString(),
                Message = miniClientData.Instance.client.file_helper.path + @$"\Images\{(string)communication_message.Data[1]}",
                DataType = "gif"
            });
        }
        private void recieveImageMessage(CommunicateMessage communication_message)
        {
            InfoDao.Save(new DataBase.Entity.Information
            {
                Owner = userData.Instance.user.uid,
                Sender = communication_message.sender,
                Receiver = communication_message.receiver,
                MDate = communication_message.date.ToString(),
                Message = miniClientData.Instance.client.file_helper.path + @$"\Images\{(string)communication_message.Data[1]}",
                DataType = "image"
            });
        }
        private void recieveFileMessage(CommunicateMessage communication_message)
        {
            InfoDao.Save(new DataBase.Entity.Information
            {
                Owner = userData.Instance.user.uid,
                Sender = communication_message.sender,
                Receiver = communication_message.receiver,
                MDate = communication_message.date.ToString(),
                Message = miniClientData.Instance.client.file_helper.path + @$"\Files\{(string)communication_message.Data[1]}",
                DataType = "file"
            });
        }
        private void deleteFriend(string uid)
        {
        }
        private void withdrawGroup(string gid)
        {
        }
    }
}
